package com.sist.ocs.component;

import com.sist.ocs.model.User;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @ClassName MyReturnCallbackSender
 * @Description TODO
 * @Author: xmk
 * @Date: 2018/10/24 14:23
 * @Version: 1.0
 */
@Component
public class MyReturnCallbackSender implements RabbitTemplate.ReturnCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send() {
        User user = new User();
        user.setPassword("222222");
        user.setUsername("tttt");

        rabbitTemplate.setReturnCallback(this);

        // 直接在sender里实现ReturnCallback接口 这种方式 Mandatory默认是true
//        rabbitTemplate.setMandatory(false);

        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());

        System.out.println(correlationData.getId());

        rabbitTemplate.convertAndSend("mydirectExchage", "myDirectRetQue_", user, correlationData);
    }

    // ReturnCallback接口用于实现消息发送到RabbitMQ交换器，但无相应队列与交换器绑定时的回调。
    @Override
    public void returnedMessage(Message message, int i, String s, String s1, String s2) {
        System.out.println("msg : " + message.getMessageProperties().getCorrelationId());
        System.out.println("msg : " + message.toString());
        System.out.println("i : " + i);
        System.out.println("s1 : " + s1);
        System.out.println("s2 : " + s2);

    }
}
